package com.itcam.interview.pro00.thread;

/**
 * @author : Cammy.Wu
 * Description : 
 */

public class WaitNotifyExample {

    public static void main(String[] args) {
        final Object monitor = new Object();

        // 消费者线程
        Thread consumerThread = new Thread(() -> {
            synchronized (monitor) {
                try {
                    System.out.println("wait消费者线程等待数据...");
                    monitor.wait();
                    System.out.println("wait消费者线程被唤醒，开始消费数据...");
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });
        consumerThread.start();

        try {
            // 主线程休眠一会儿，确保消费者线程进入等待状态
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        // 生产者线程
        Thread producerThread = new Thread(() -> {
            synchronized (monitor) {
                System.out.println("notify生产者生产数据...");
                // 模拟生产数据的操作
                monitor.notify();
                System.out.println("notify生产者通知消费者线程...");
            }
        });

        producerThread.start();
    }
}
